# ---------------------------------------------------------
cmake_minimum_required(VERSION 2.8)

MACRO(ADD_MSVC_PRECOMPILED_HEADER PrecompiledHeader PrecompiledSource SourcesVar)
  IF(MSVC)
    GET_FILENAME_COMPONENT(PrecompiledBasename ${PrecompiledHeader} NAME_WE)
    SET(PrecompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${PrecompiledBasename}.pch")
    SET(__Sources ${${SourcesVar}})

    SET_SOURCE_FILES_PROPERTIES(${PrecompiledSource}
                                PROPERTIES COMPILE_FLAGS "/Yc\"${PrecompiledHeader}\" /Fp\"${PrecompiledBinary}\""
                                           OBJECT_OUTPUTS "${PrecompiledBinary}")
    SET_SOURCE_FILES_PROPERTIES(${__Sources}
                                PROPERTIES COMPILE_FLAGS "/Yu\"${PrecompiledHeader}\" /FI\"${PrecompiledHeader}\" /Fp\"${PrecompiledBinary}\""
                                           OBJECT_DEPENDS "${PrecompiledBinary}")  
    # Add precompiled header to SourcesVar
    #LIST(APPEND ${SourcesVar} ${PrecompiledSource})
  ENDIF(MSVC)
ENDMACRO(ADD_MSVC_PRECOMPILED_HEADER)

macro ( mark_as_internal _var )
	set ( ${_var} ${${_var}} CACHE INTERNAL "hide this!" FORCE )
endmacro( mark_as_internal _var )

macro ( mark_as_disable _var )
	set ( ${_var} OFF CACHE BOOL "disable this!" FORCE )
endmacro( mark_as_disable _var )

macro ( mark_as_disable_and_internal _var )
	set ( ${_var} OFF CACHE INTERNAL "disable and hide this!" FORCE )
endmacro( mark_as_disable_and_internal _var )

macro ( mark_as_enable_and_internal _var )
	set ( ${_var} ON CACHE INTERNAL "enable and hide this!" FORCE )
endmacro( mark_as_enable_and_internal _var )

macro ( mark_as_empty _var )
	set ( ${_var} "" CACHE STRING "disable this!" FORCE )
endmacro( mark_as_empty _var )

macro ( mark_as_empty_and_internal _var )
	set ( ${_var} "" CACHE INTERNAL "disable and hide this!" FORCE )
endmacro( mark_as_empty_and_internal _var )

function(ns_configure_executable TYPE)

	# first step: build the primary and separate lists
	set(_PRIMARY "")
	set(_CONTENT "")
	set(_SEP FALSE)
	
	foreach(_FILE ${ARGN})
		if (_FILE STREQUAL "SEPARATE")
			set(_SEP TRUE)
		else ()
			if (_SEP)
				list(APPEND _CONTENT ${_FILE})
			else ()
				list(APPEND _PRIMARY ${_FILE})
			endif ()
		endif()
	endforeach()
	
	set(_SOURCES ${_PRIMARY} )
	
	list(REMOVE_DUPLICATES _SOURCES)
	
	include_directories(${NSCORE_INCLUDE_DIRS})
	add_executable(${PROJECT_NAME} ${TYPE} ${_SOURCES} ${_CONTENT})
	target_link_libraries(${PROJECT_NAME} ${NS_CORE_NAME})

	if(APPLE)
		set(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -framework AppKit -framework CoreData")
	endif()


	if(WIN32)
		target_link_libraries(${PROJECT_NAME} ole32.lib comctl32.lib gdiplus.lib)
	else()
	
		if (UNIX AND NOT NS_BUILD_PLATFORM_APPLE_IOS)
			
			find_package(X11)
			macro_log_feature(X11_FOUND "X11" "X Window system" "http://www.x.org" TRUE "" "")
			target_link_libraries(${PROJECT_NAME} ${X11_LIBRARIES} )
			
			if(USE_MOTIF)
				
				find_package(Motif)
				
				macro_log_feature(MOTIF_FOUND "MOTIF" "MOTIF" TRUE "" "")
				
				target_link_libraries(${PROJECT_NAME} ${MOTIF_LIBRARIES})
				
			else()
				
				find_package(GTK2 COMPONENTS gtk)
				
				macro_log_feature(GTK2_FOUND "GTK2" "GTK2" TRUE "" "")
				
				target_link_libraries(${PROJECT_NAME} ${GTK2_ATK_LIBRARY} ${GTK2_CAIRO_LIBRARY} ${GTK2_GDK_LIBRARY} ${GTK2_GDK_PIXBUF_LIBRARY} ${GTK2_GTK_LIBRARY} ${GTK2_PANGO_LIBRARY} ${GTK2_LIBRARIES})

			endif()
			
			macro_display_feature_log()

		endif ()
		
	endif()
	

	foreach(_FILE ${_CONTENT})
		
		ADD_CUSTOM_COMMAND(
			TARGET ${PROJECT_NAME}
			POST_BUILD
			COMMAND ${CMAKE_COMMAND} ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_FILE}
		)
		
	endforeach()
	
	
endfunction()

function(ns_configure_external_library)

	INSTALL(TARGETS ${PROJECT_NAME}	
		DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/Debug
		CONFIGURATIONS Debug
		PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
	)

	INSTALL(TARGETS ${PROJECT_NAME}	
		DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/Release
		CONFIGURATIONS Release
		PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
	)

	if(APPLE)
		install(FILES ${PUBLIC_HEADERS_FILES} DESTINATION  ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME})

		ADD_CUSTOM_COMMAND(
			TARGET ${PROJECT_NAME}
			POST_BUILD
			COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_CFG_INTDIR})

	endif()

endfunction()


function(ns_configure_library)

	INSTALL(TARGETS ${PROJECT_NAME}	
		DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/Debug
		CONFIGURATIONS Debug
		PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
	)

	INSTALL(TARGETS ${PROJECT_NAME}	
		DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/Release
		CONFIGURATIONS Release
		PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
	)

	if(APPLE)

		install(FILES ${PUBLIC_HEADERS_FILES} DESTINATION  ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME})

		ADD_CUSTOM_COMMAND(
			TARGET ${PROJECT_NAME}
			POST_BUILD
			COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_CFG_INTDIR})

	endif()
	
endfunction()



# function(configure_executable TARGETNAME)

	# # first step: build the primary and separate lists
	# set(_PRIMARY "")
	# set(_CONTENT "")
	# set(_SEP FALSE)
	# foreach(_FILE ${ARGN})
		# if (_FILE STREQUAL "SEPARATE")
			# set(_SEP TRUE)
		# else ()
			# if (_SEP)
				# list(APPEND _CONTENT ${_FILE})
			# else ()
				# list(APPEND _PRIMARY ${_FILE})
			# endif ()
		# endif()
	# endforeach()
	
	# set(_SOURCES ${_PRIMARY} )
	
	# list(REMOVE_DUPLICATES _SOURCES)
	
	# message("Source for ${TARGETNAME} are ${_SOURCES}")

	# if (APPLE)
	
		# ogre_add_executable(${TARGETNAME} MACOSX_BUNDLE ${_SOURCES})

		# include_directories(${ENGINE_OSX_DIRS})
		
		# set_target_properties(${TARGETNAME} PROPERTIES MACOSX_BUNDLE TRUE)
		
		# set_target_properties(${TARGETNAME} PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME ${TARGETNAME})

		# set(EXECUTABLE_NAME ${TARGETNAME})

		# message("OgreFramework Path = ${OGRE_FRAMEWORK_PATH}")
		
		# if(OGRE_BUILD_PLATFORM_APPLE_IOS)
		
			# set_target_properties(${TARGETNAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/IOS/Info.plist)

			# SET_SOURCE_FILES_PROPERTIES(IOS/WindowController.xib PROPERTIES MACOSX_PACKAGE_LOCATION Resources)


			# set(IOS_RESOURCES
				# ${ENGINE_SOURCE_DIR}/IOS/icon.png
				# ${ENGINE_SOURCE_DIR}/IOS/icon@2x.png
				# ${ENGINE_SOURCE_DIR}/IOS/icon-72.png
			# )
		
			# include_directories(${ENGINE_SOURCE_DIR}/IOS)
			
			# SET_SOURCE_FILES_PROPERTIES(${IOS_RESOURCES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources )

			# set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.yourcompany.\${PRODUCT_NAME:rfc1034identifier}")		
		
			# set(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -framework CoreGraphics -framework QuartzCore -framework UIKit")
			
			# set(OGRE_CONFIG_ENABLE_VIEWPORT_ORIENTATIONMODE TRUE CACHE BOOL "Forcing viewport orientation support for iPhone" FORCE)
			
			# set(CMAKE_OSX_ARCHITECTURES "armv7")
			# set(XCODE_ATTRIBUTE_SDKROOT iphoneos)
			
			# #set_target_properties(${TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_GCC_THUMB_SUPPORT "NO")
			# #set_target_properties(${TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_GCC_UNROLL_LOOPS "YES")
			# set_target_properties(${TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer")
			# #set_target_properties(${TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
			

			# remove_definitions(-msse)
			
		# else(OGRE_BUILD_PLATFORM_APPLE_IOS)

			# set_target_properties(${TARGETNAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/OSX/Info.plist)

			# SET_SOURCE_FILES_PROPERTIES(OSX/MainMenu.xib PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
					
			# set_target_properties(${TARGETNAME} PROPERTIES
				# COMPILE_FLAGS "-F${OGRE_FRAMEWORK_PATH}"
				# LINK_FLAGS "-F${OGRE_FRAMEWORK_PATH} -framework IOKit -framework Cocoa -framework Carbon -framework OpenGL -framework CoreVideo"
		  # )
			
		# endif(OGRE_BUILD_PLATFORM_APPLE_IOS)

	# endif(APPLE)
	
	# if (WIN32)
	
		# ogre_add_executable(${TARGETNAME} WIN32 ${_SOURCES})
		
	# endif(WIN32)
	
	# unset(APPLE_RESOURCES_CONTENT CACHE)
	
	# foreach(_FILE ${_CONTENT})
		
		# find_file(_PATH_FILE ${_FILE} HINTS ${CMAKE_CURRENT_SOURCE_DIR} ${OGRE_MEDIA_DIR}/models/)
		
		# set(APPLE_RESOURCES_CONTENT ${APPLE_RESOURCES_CONTENT} " " ${_PATH_FILE})

		# message("Post build copying content file: ${_PATH_FILE}")
		
		# if (WIN32)

			# ADD_CUSTOM_COMMAND(
				# TARGET ${TARGETNAME}
				# POST_BUILD
				# COMMAND ${CMAKE_COMMAND} ARGS -E copy ${_PATH_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_FILE}
			# )
		
		# endif(WIN32)
		
		# unset(_PATH_FILE CACHE)

	# endforeach()
	
	#message(${APPLE_RESOURCES_CONTENT})

	# if (APPLE)		
	
		# SET_SOURCE_FILES_PROPERTIES(${APPLE_RESOURCES_CONTENT} PROPERTIES MACOSX_PACKAGE_LOCATION Resources )
	
	# endif(APPLE)

# endfunction()

# function(install_external TARGETNAME)

	# set(EXTERNAL_DIR "")
	
	# set(_SEP FALSE)
	
	# foreach(_FILE ${ARGN})
		
		# if (_FILE STREQUAL "SEPARATE")
			
			# set(_SEP TRUE)
			
		# elseif (_SEP)
			
				# if (APPLE)
					# set(FULL_FILE lib${_FILE}.a)
				# else (APPLE)
					# set(FULL_FILE ${CMAKE_LINK_LIBRARY_PREFIX}${_FILE}${CMAKE_LINK_LIBRARY_SUFFIX})
				# endif (APPLE)
		
				# ADD_CUSTOM_COMMAND(
					# TARGET ${TARGETNAME}
					# POST_BUILD
					# COMMAND ${CMAKE_COMMAND} ARGS -E copy ${EXTERNAL_DIR}/${FULL_FILE} ${NSLINK_INSTALL_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE}
				# )
				
				#message("copy ${EXTERNAL_DIR}/${FULL_FILE} ${NSLINK_INSTALL_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE}")
				
				# unset(FULL_FILE CACHE)
				
		# else(_SEP)

			# set(EXTERNAL_DIR ${_FILE})
		
		# endif()
		
	# endforeach()

# endfunction()

# function(install_shared_library TARGETNAME)

	# INSTALL(TARGETS ${TARGETNAME}
		# RUNTIME DESTINATION ${NSLINK_INSTALL_DIR}
		# LIBRARY DESTINATION ${NSLINK_INSTALL_DIR}
		# ARCHIVE DESTINATION ${NSLINK_INSTALL_DIR}
		# FRAMEWORK DESTINATION ${NSLINK_INSTALL_DIR}
		# CONFIGURATIONS 
	# )
	
	# ADD_CUSTOM_COMMAND(
		# TARGET ${TARGETNAME}
		# POST_BUILD
		# COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE} ${NSLINK_INSTALL_DIR}/${CMAKE_CFG_INTDIR}/
	# )

	#message("COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE} ${NSLINK_INSTALL_DIR}/${CMAKE_CFG_INTDIR}/")

# endfunction()


# function(install_static_library TARGETNAME)

	# set(FULL_FILE ${CMAKE_STATIC_LIBRARY_PREFIX}${TARGETNAME}${CMAKE_STATIC_LIBRARY_SUFFIX})	
	
	# ADD_CUSTOM_COMMAND(
		# TARGET ${TARGETNAME}
		# POST_BUILD
		# COMMAND ${CMAKE_COMMAND} ARGS -E copy ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE} ${NSLINK_INSTALL_DIR}/${CMAKE_CFG_INTDIR}/${FULL_FILE}
	# )
	
# endfunction()

